﻿using System;
using System.Collections.Generic;
using System.Text;

using SugarWebApi.Commands;

namespace SugarWebApi.Processors
{

	public class SoapV2Processor : SoapProcessor
	{

		#region Constructors

		public SoapV2Processor(
			string url) : base(url)
		{
		}

		#endregion

		#region Fields

		private readonly SugarSoapV2.sugarsoap _soapV2 = new SugarSoapV2.sugarsoap();

		#endregion

		#region Propreties

		public virtual string SoapEndPoint
		{
			get 
			{
				string format = (this.Url.EndsWith("/")) ? "" : this.Url + "/";
				format += "{0}service/v2/soap.php";
				return string.Format(format, this.Url);
			}
		}

		#endregion

		protected override void OnUrlChanged(EventArgs e)
		{
			this._soapV2.Url = this.SoapEndPoint;
		}

		protected override LoginResult ProcesLoginCommand(
			LoginCommand command)
		{
			SugarSoapV2.user_auth auth = new SugarWebApi.SugarSoapV2.user_auth();
			auth.user_name = command.Parameters.Username;
			auth.password = command.Parameters.PasswordDigest;

			SugarSoapV2.entry_value soapresult =
				this._soapV2.login(
					auth,
					"SugarWebApi",
					null);

			LoginResult result = new LoginResult(
				command,
				!string.IsNullOrEmpty(soapresult.id),
				soapresult.id);

			return result;
		}

		protected override GetAvailableModulesResult ProcesGetAvailableModeulsCommand(
			GetAvailableModulesCommand command)
		{
			SugarSoapV2.module_list soapresult =
				this._soapV2.get_available_modules(
					command.Parameters.SessionId);

			GetAvailableModulesResult result = new GetAvailableModulesResult(
				command,
				!(soapresult.modules != null),
				SoapV2Translator.ToSugarModules(command.Conext, soapresult.modules));

			return result;
		}

		protected override LogoutResult ProcesLogoutCommand(LogoutCommand command)
		{
			this._soapV2.logout(
					command.SessionId);

			LogoutResult result = new LogoutResult(
				command,
				true);

			return result;
		}

		protected override GetModuleFieldsResult ProcesGetModuleFieldsCommand(
			GetModuleFieldsCommand command)
		{
			SugarSoapV2.new_module_fields soapresult =
				this._soapV2.get_module_fields(
					command.SessionId,
					command.Module,
					null);

			GetModuleFieldsResult result = new GetModuleFieldsResult(
				command,
				!(string.IsNullOrEmpty(soapresult.module_name)),
				SoapV2Translator.ToSugarFields(command.Conext, soapresult.module_fields));
		
			return result;
		}

		protected override GetEntryListResult ProcesGetEntryListCommand(GetEntryListCommand command)
		{
			SugarSoapV2.get_entry_list_result_version2 soapresult =
				this._soapV2.get_entry_list(
				command.SessionId,
				command.Module,
				command.Query,
				command.OrderBy,
				command.Offset,
				command.Fields,
				new SugarWebApi.SugarSoapV2.link_name_to_fields_array[] { },
				command.Limit,
				(command.Deleted) ? 1 : 0);

			SugarIndexResolver resolver = new SugarIndexResolver(command.Fields);
			GetEntryListResult result = new GetEntryListResult(
				command,
				true,
				command.Fields,
				SoapV2Translator.ToSugarEntries(soapresult.entry_list, resolver));

			return result;
		}

		protected override SetEntryResult ProcesSetEntryCommand(SetEntryCommand command)
		{
			throw new NotImplementedException();
		}
	}
}
